package com.it.controller;

import com.it.pojo.Article;
import com.it.pojo.Category;
import com.it.pojo.PageBean;
import com.it.pojo.Result;
import com.it.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    //新增文章
    @PostMapping
    public Result add(@RequestBody @Validated Article article) {
        System.out.println("接收数据：" + article);
        articleService.add(article);
        return Result.success();
    }

    @GetMapping
    public Result<PageBean<Article>> list(
            Integer pageNum,
            Integer pageSize,
            @RequestParam(required = false) Integer categoryId,
            @RequestParam(required = false) String state
    ){

       PageBean<Article> pb =  articleService.list(pageNum,pageSize,categoryId,state);
       return Result.success(pb);
    }

    @PutMapping
    public Result update(@RequestBody  Article article){
        articleService.update(article);
        return Result.success();
    }


    @DeleteMapping
    public Result deleteArticle(@RequestParam Integer id){
        articleService.deleteArticle(id);
        return  Result.success();
    }

}
